package com.example.demo.controller;


import com.example.demo.entity.Resource;
import com.example.demo.entity.Resource_apply;
import com.example.demo.entity.Resourcetype;
import com.example.demo.pojo.PlannerResult;
import com.example.demo.repository.ActivityRepository;
import com.example.demo.repository.ResourceRepository;
import com.example.demo.repository.Resource_applyRepository;
import com.example.demo.service.ResourceService;
import com.example.demo.utils.ThreadLocalUtil;
import com.example.demo.vo.ResourceVo;
import com.example.demo.vo.Resource_applyVo;
import com.example.demo.vo.ResourcetypeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/resource")
public class ResourceController {

    @Autowired
    ResourceService resourceService;

    @Autowired
    Resource_applyRepository resource_applyRepository;

    @Autowired
    ResourceRepository resourceRepository;
    @Autowired
    private ActivityRepository activityRepository;

    @GetMapping("/type")
    PlannerResult<List<Resourcetype>> all_resource_type()
    {
        return resourceService.all_resource_type();
    };

    @PostMapping("/addresourcetype")
    PlannerResult<String> add_resource_type(@RequestBody ResourcetypeVo resourcetypeVo){
        return resourceService.add_resource_type(resourcetypeVo);
    };

    @PostMapping("/add")
    PlannerResult<String> add_resource(@RequestBody ResourceVo resourceVo){
        return resourceService.add_resource(resourceVo);
    };

    @PostMapping("/applyresource")
    PlannerResult<Resource> apply_resource(@RequestBody Resource_applyVo resourceApplyVo){
        return resourceService.apply_resource(resourceApplyVo);
    };

    @GetMapping("/detectresource")
    PlannerResult<Resource> detect_resource(@RequestParam ("applyid") Integer applyid){
        return resourceService.detect_resource(applyid);
    };


    //新增简单查询所有apply，在controller直接实现
    @PostMapping("/allresourceapply")
    PlannerResult<List<Resource_apply>> all_resource_apply(){
        return PlannerResult.success(resource_applyRepository.findAll());
    }

    //用户用id找自己的申请
    @PostMapping("/myresourceapply")
    PlannerResult <List<Resource_applyVo>> my_resource_apply(){
        Map<String,Object> user= ThreadLocalUtil.get();
        Integer userid=(Integer)user.get("id");
        List<Resource_apply> all = resource_applyRepository.findAllByApplyerId(userid);
        List<Resource_applyVo> applyVoList=new ArrayList<>();
        for (Resource_apply resource_apply:all) {
            Resource_applyVo resource_applyVo=new Resource_applyVo();
            resource_applyVo.setId(resource_apply.getId());
            resource_applyVo.setResource_name(resourceRepository.findById(resource_apply.getResource_id()).get().getName());
            resource_applyVo.setActivity_name(activityRepository.findById(resource_apply.getActivity_id()).get().getName());
            resource_applyVo.setNum(resource_apply.getNum());
            resource_applyVo.setState(resource_apply.getState());
            applyVoList.add(resource_applyVo);
        }
        return PlannerResult.success(applyVoList);
    }

    @PostMapping("/list")
    PlannerResult<Page<Resource>> all_resourcetype (@RequestBody Map<String, Integer> params)
    {    Integer page = params.get("page");
        Integer size = params.get("size");
        Pageable pageable = PageRequest.of(page - 1, size);
        Page<Resource> result = resourceRepository.findAll(pageable);
        return PlannerResult.success(result);

    }

    @GetMapping("/all")
    PlannerResult<List<Resource>> all_resource ()
    {
        List<Resource> result = resourceRepository.findAll();
        return PlannerResult.success(result);
    }

    @PostMapping("/delete/{resourceid}")
    public PlannerResult<String> deleteUser(@PathVariable("resourceid") Integer resourceid) {
        resourceRepository.deleteById(resourceid);
        if (resourceRepository.existsById(resourceid)) {
            return PlannerResult.error("删除失败");
        } else {
            return PlannerResult.success("删除成功");
        }
    }
}
